<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>从结果集中取得一行作为对象返回</title>
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-base.css" />
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-medium.css" />

 </head>
 <body class="docs"><div class="navbar navbar-fixed-top">
  <div class="navbar-inner clearfix">
    <ul class="nav" style="width: 100%">
      <li style="float: left;"><a href="function.mysql-fetch-lengths.html">« mysql_fetch_lengths</a></li>
      <li style="float: right;"><a href="function.mysql-fetch-row.html">mysql_fetch_row »</a></li>
    </ul>
  </div>
</div>
<div id="breadcrumbs" class="clearfix">
  <ul class="breadcrumbs-container">
    <li><a href="index.html">PHP Manual</a></li>
    <li><a href="ref.mysql.html">MySQL 函数</a></li>
    <li>从结果集中取得一行作为对象返回</li>
  </ul>
</div>
<div id="layout">
  <div id="layout-content"><div id="function.mysql-fetch-object" class="refentry">
   <div class="refnamediv">
    <h1 class="refname">mysql_fetch_object</h1>
    <p class="verinfo">(PHP 4, PHP 5)</p><p class="refpurpose"><span class="refname">mysql_fetch_object</span> &mdash; <span class="dc-title">从结果集中取得一行作为对象返回</span></p>

   </div>
 <div id="function.mysql-fetch-object-refsynopsisdiv">
  <div class="warning"><strong class="warning">警告</strong>
   <p class="para">本扩展自 PHP 5.5.0
起已废弃，并在自 PHP 7.0.0 开始被移除。应使用 <a href="book.mysqli.html" class="link">MySQLi</a>
或 <a href="ref.pdo-mysql.html" class="link">PDO_MySQL</a> 扩展来替换之。参见 <a href="mysqlinfo.api.choosing.html" class="link">MySQL：选择 API</a> 
指南来获取更多信息。用以替代本函数的有：</p>
   <ul class="simplelist">
    <li class="member"><span class="function"><a href="mysqli-result.fetch-object.html" class="function">mysqli_fetch_object()</a></span></li>
    <li class="member"><span class="methodname"><a href="pdostatement.fetch.html" class="methodname">PDOStatement::fetch(PDO::FETCH_OBJ)</a></span></li>
   </ul>
  </div>
 </div>
 <div class="refsect1 description" id="refsect1-function.mysql-fetch-object-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
    <span class="methodname"><strong>mysql_fetch_object</strong></span>(<span class="methodparam"><span class="type">resource</span> <code class="parameter">$result</code></span>, <span class="methodparam"><span class="type">string</span> <code class="parameter">$class_name</code><span class="initializer"> = ?</span></span>, <span class="methodparam"><span class="type">array</span> <code class="parameter">$params</code><span class="initializer"> = ?</span></span>): <span class="type">object</span></div>

  <p class="para rdfs-comment">
    返回一个对象，其属性与获取的行相对应，并将内部数据指针向前移动。
  </p>
 </div>



 <div class="refsect1 parameters" id="refsect1-function.mysql-fetch-object-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>
    <dt>

<code class="parameter">result</code></dt>
<dd>
<p class="para"><span class="type">resource</span>
型的结果集。此结果集来自对 <span class="function"><a href="function.mysql-query.html" class="function">mysql_query()</a></span>
的调用。</p></dd>

    
     <dt>
<code class="parameter">class_name</code></dt>

      <dd>

       <p class="para">
        要实例化、设置属性并返回的类的名称，如果不指定，默认返回
        <span class="classname"><a href="class.stdclass.html" class="classname">stdClass</a></span> 对象。
       </p>
     </dd>

    
    
     <dt>
<code class="parameter">params</code></dt>

      <dd>

       <p class="para">
        可选 <span class="type">array</span> 数组参数，会传递给 <code class="parameter">class_name</code> 类的构造函数。
       </p>
     </dd>

    
   </dl>

  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.mysql-fetch-object-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
    返回根据所取得的行生成的对象 <span class="type">object</span>，如果没有更多行则返回 <strong><code>false</code></strong>。
  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-function.mysql-fetch-object-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="example-1756">
    <p><strong>示例 #1 <span class="function"><strong>mysql_fetch_object()</strong></span> example</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />mysql_connect</span><span style="color: #007700">(</span><span style="color: #DD0000">"hostname"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"user"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"password"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">mysql_select_db</span><span style="color: #007700">(</span><span style="color: #DD0000">"mydb"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$result&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">mysql_query</span><span style="color: #007700">(</span><span style="color: #DD0000">"select&nbsp;*&nbsp;from&nbsp;mytable"</span><span style="color: #007700">);<br />while&nbsp;(</span><span style="color: #0000BB">$row&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">mysql_fetch_object</span><span style="color: #007700">(</span><span style="color: #0000BB">$result</span><span style="color: #007700">))&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #0000BB">$row</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">user_id</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #0000BB">$row</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">fullname</span><span style="color: #007700">;<br />}<br /></span><span style="color: #0000BB">mysql_free_result</span><span style="color: #007700">(</span><span style="color: #0000BB">$result</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

   </div>
  </p>
  <p class="para">
   <div class="example" id="example-1757">
    <p><strong>示例 #2 <span class="function"><strong>mysql_fetch_object()</strong></span> example</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">class&nbsp;</span><span style="color: #0000BB">foo&nbsp;</span><span style="color: #007700">{<br />&nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;</span><span style="color: #0000BB">$name</span><span style="color: #007700">;<br />}<br /><br /></span><span style="color: #0000BB">mysql_connect</span><span style="color: #007700">(</span><span style="color: #DD0000">"hostname"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"user"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"password"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">mysql_select_db</span><span style="color: #007700">(</span><span style="color: #DD0000">"mydb"</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$result&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">mysql_query</span><span style="color: #007700">(</span><span style="color: #DD0000">"select&nbsp;name&nbsp;from&nbsp;mytable&nbsp;limit&nbsp;1"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$obj&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">mysql_fetch_object</span><span style="color: #007700">(</span><span style="color: #0000BB">$result</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'foo'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$obj</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

   </div>
  </p>
 </div>


 <div class="refsect1 notes" id="refsect1-function.mysql-fetch-object-notes">
  <h3 class="title">注释</h3>
  <blockquote class="note"><p><strong class="note">注意</strong>: 
   <strong>Performance</strong><br />
   <p class="para">
     速度上，本函数和
     <span class="function"><a href="function.mysql-fetch-array.html" class="function">mysql_fetch_array()</a></span> 一样，也几乎和
     <span class="function"><a href="function.mysql-fetch-row.html" class="function">mysql_fetch_row()</a></span> 一样快（差别很不明显）。
   </p>
  </p></blockquote>
  <blockquote class="note"><p><strong class="note">注意</strong>: 
   <p class="para">
     <span class="function"><strong>mysql_fetch_object()</strong></span> 和
     <span class="function"><a href="function.mysql-fetch-array.html" class="function">mysql_fetch_array()</a></span>
     类似，只有一点区别 - 返回一个对象而不是数组。间接地也意味着只能通过字段名来访问数组，而不是偏移量（数字是合法的属性名）。
   </p>
  </p></blockquote>
  <blockquote class="note"><p><strong class="note">注意</strong>: <span class="simpara">此函数返回的字段名<em class="emphasis">大小写敏感</em>。</span></p></blockquote>
  <blockquote class="note"><p><strong class="note">注意</strong>: <span class="simpara">此函数将 NULL 字段设置为 PHP <strong><code>null</code></strong> 值。</span></p></blockquote>
 </div>


 <div class="refsect1 seealso" id="refsect1-function.mysql-fetch-object-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"><span class="function"><a href="function.mysql-fetch-array.html" class="function" rel="rdfs-seeAlso">mysql_fetch_array()</a> - 从结果集中取得一行作为关联数组</span></li>
    <li class="member"><span class="function"><a href="function.mysql-fetch-assoc.html" class="function" rel="rdfs-seeAlso">mysql_fetch_assoc()</a> - 从结果集中取得一行作为关联数组</span></li>
    <li class="member"><span class="function"><a href="function.mysql-fetch-row.html" class="function" rel="rdfs-seeAlso">mysql_fetch_row()</a> - 从结果集中取得一行作为枚举数组</span></li>
    <li class="member"><span class="function"><a href="function.mysql-data-seek.html" class="function" rel="rdfs-seeAlso">mysql_data_seek()</a> - 移动内部结果的指针</span></li>
    <li class="member"><span class="function"><a href="function.mysql-query.html" class="function" rel="rdfs-seeAlso">mysql_query()</a> - 发送一条 MySQL 查询</span></li>
   </ul>
  </p>
 </div>

</div></div></div></body></html>